//
//  NoticeDetailViewController.m
//  Contacts
//
//  Created by 武 娇 on 14-8-5.
//  Copyright (c) 2014年 李 雷川. All rights reserved.
//

#import "NoticeDetailViewController.h"
#import "Notice.h"
#import "ServerInterface.h"
#import "Contact.h"
#import "IndicativeView.h"
extern Contact *globalContact;
@interface NoticeDetailViewController ()<UIWebViewDelegate>{
    IBOutlet UIWebView *webView;
    IndicativeView *indicativeView;
    Notice  *notice;
}

@end

@implementation NoticeDetailViewController
@synthesize finishLoadBlock;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    webView.backgroundColor = [UIColor clearColor];//清除背景色
    webView.opaque = NO;//背景不透明设置为NO
    indicativeView = [[IndicativeView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:indicativeView];
    [indicativeView showFriendlyLoadingViewWithText:@"正在加载..." loadingAnimated:YES];
    
    self.view.backgroundColor = [UIColor colorWithRed:246.0/255 green:246.0/255 blue:246.0/255 alpha:1.0];
//    if (notice.content  && notice.content.length > 0) {
//        [self setupWebView];
//    }
//    else{
        NSDictionary *noticeDetailDic = @{@"company_id":CompanyID,@"token":globalContact.token,@"user_id":@(globalContact.userID),@"msg_id":@(notice.ID)};
        [ServerInterface getNoticeDetailWithDic:noticeDetailDic withBlock:^(NSDictionary *newDic, NSError *error) {

            
           // NSLog(@"noticeDetailDic is:%@",newDic);
            if (error) {
                NSLog(@"error is:%@",error);
            }
            else{
                if ([[newDic valueForKeyPath:@"success"] boolValue]) {
                    NSDictionary *contentDic = newDic[@"data"];
                    notice.content = contentDic[@"content"];
                    [self setupWebView];
                    [Notice updateNoticeContent:notice];
                }
                else{
                    
                }
            }
        }];

//}
}

-(void)viewWillAppear:(BOOL)animated{
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    self.title =@"通知";
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)initNotice:(Notice *)_notice{
    notice = _notice;
    
}
-(void)setupWebView{
//    webView.scalesPageToFit = YES;
  
    NSString *style = @"<style>img{max-width:100%;}</style>";
    NSString *html =[NSString stringWithFormat:@"<body>%@%@</body>",style,notice.content];
    [webView loadHTMLString:html baseURL:nil];

}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
#pragma mark
#pragma mark UIWebViewDelegate
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    return YES;
}
- (void)webViewDidStartLoad:(UIWebView *)webView{
    
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
    if (indicativeView) {
        [indicativeView hideLoadingView];
    }
    if (finishLoadBlock) {
        self.finishLoadBlock();
    }

}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    if (indicativeView) {
        [indicativeView hideLoadingView];
    }
}
@end
